<?php
/**
 * 公用函数库
 *
 */
class Helper_Common {
	/**
	 * 系统加密算法
	 *
	 * @param 加密原串 $var
	 * @return string
	 */
	public static function system_md5($var)
	{
		$_md5 = md5($var);
		$b = array();
		for ($i=0 ; $i<32 ; $i+=2)
		{
			if (fmod($i,3)){
				$b[] = strrev(substr($_md5,$i,2));
			}else{
				$b[] = substr($_md5,$i,2);
			}
		}
		return md5(implode('',$b));
	}
	static function cget($url)
	{
		$cu = curl_init();
		curl_setopt($cu, CURLOPT_URL, $url);
		curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
		$ret = curl_exec($cu);
		curl_close($cu);
		return $ret;
	}
	/*
	 *访问URL
	 */
	static function curl($url, $postFields = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			$postMultipart = false;
			foreach ($postFields as $k => $v)
			{
				if("@" != substr($v, 0, 1))//判断是不是文件上传
				{
					$postBodyString .= "$k=" . urlencode($v) . "&"; 
				}
				else//文件上传用multipart/form-data，否则用www-form-urlencoded
				{
					$postMultipart = true;
				}
			}
			unset($k, $v);
			curl_setopt($ch, CURLOPT_POST, true);
			if ($postMultipart)
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
			}
			else
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
			}
		}
		$reponse = curl_exec($ch);
		
		if (curl_errno($ch))
		{
			throw new Exception(curl_error($ch),0);
		}
		else
		{
			$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode)
			{
				throw new Exception($reponse,$httpStatusCode);
			}
		}
		curl_close($ch);
		return $reponse;
	}
/**
	 * 
	 *2011-12-30-下午05:30:52 by 460932465
	 * 返回格式化后的字符串
	 * @param 左边字符 $leftchar
	 * @param 右边字符 $rightchar
	 * @param 完整字符串 $string
	 */
	static function get_split($leftchar,$rightchar,$string)
	{
		$a =  strpos($string,$leftchar) ;
		$b = strpos($string,$rightchar,$a);
		if($a === false && $b === false)
		{
			return false;
		}else
		{
			$c = substr($string,$a + strlen($leftchar),$b - $a - strlen($leftchar));
			$c = str_replace("\n","",$c);
			$c = str_replace("\r","",$c);
			$c = str_replace("\r\n","",$c);
			return $c;
		}
	}
	static function get_rnd($type,$len)
	{
		$key = array(
			0=>'0123456789'
		);
		$result = '';
		for ($i=1;$i<=$len;$i++)
		{
			$result .= rand(0, 9);	
		}
		return $result;
	}
/*
     * 简单的日志功能
     */

    public static function log($str, $file='common') {
        if (!$file || !$str)
            return false;
		$file .= '_'.date('Y-m-d');
        $file = Q::ini('app_config/ROOT_DIR') . "/log/{$file}.log";
         touch($file);
        return file_put_contents($file, date('Ymd H:i:s') . ' ' . $str . "\n", FILE_APPEND);
    }
    public static function get_ip()
    {
    	if(isset($_SERVER['HTTP_X_REAL_IP']) && $_SERVER['HTTP_X_REAL_IP']<>'')
    	{
    		$onlineip = htmlentities($_SERVER['HTTP_X_REAL_IP']);
    	}else if(isset($_SERVER['REMOTE_ADDR'])){
    		$onlineip = $_SERVER['REMOTE_ADDR'];
    	}else{
    	    $onlineip = '127.0.0.1';
    	}
    	return $onlineip;
    }
    public static function trim($acctname)
    {
    	$acctname = str_replace('+86', '', $acctname);
    	return trim($acctname);
    }
}